import sys
import linuxcnc
from PySide import QtCore, QtGui
from powerWidget import PowerWidget
from droWidget import DroWidget
from manualWidget import ManualWidget




class MainWindow(QtGui.QMainWindow):
	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self, parent)
		#emc connection
		self.emcStat = linuxcnc.stat()
		self.emcStat.poll()
		self.emcCom = linuxcnc.command()
		
		#Init Gui  
		self.createActions()
		self.initGui()
		
		#timer updating widgets		
		self.timer = QtCore.QTimer(self)
                self.timer.timeout.connect(self.updateStats)
                self.timer.start(100)
	

	## GUI SECTION ##
	       	
        def initGui(self):
        	#create active widgets
        	self.powerWidget = PowerWidget(self)
        	self.dro = DroWidget(self)
        	self.manual = ManualWidget(self)
        	
        	
        	#list of updateable widgets
        	self.widgetList = []
        	self.widgetList.append(self.dro)
        	self.widgetList.append(self.powerWidget)
		
		#MDI area
		self.mdi = QtGui.QMdiArea()
		self.mdi.addSubWindow(self.dro)
		self.mdi.addSubWindow(self.manual)
		self.setCentralWidget(self.mdi)
		
		self.createMenus()
		self.createToolsBar()

	def createMenus(self):
		self.fileMenu =  self.menuBar().addMenu(self.tr("&File"))
		self.fileMenu.addAction(self.openAct)
		
	def createActions(self):
		#menu actions
		self.openAct = QtGui.QAction(self.tr("&Open gFile"), self)
        	self.openAct.setShortcut(self.tr("Ctrl+O"))
        	self.openAct.setStatusTip(self.tr("Open a new file"))
        	self.connect(self.openAct, QtCore.SIGNAL("triggered()"), self.openFile)
		
	def createToolsBar(self):
		self.toolbar = self.addToolBar("toolsbar")
		self.toolbar.addWidget(self.powerWidget)
		self.toolbar.orientationChanged.connect(self.toolbarOrientationChanged)
		self.toolbar.addSeparator()
        		
        def openFile(self):
        	self.filename = QtGui.QFileDialog.getOpenFileName(self, "Open File", QtCore.QDir.currentPath(), "G Code (*.ngc);;All Files(*.*)")
        	print self.filename[0]
        	
        def toolbarOrientationChanged(self):
        	self.powerWidget.orientationChanged()
        	
        def showMessage(self, message):
        	msgBox = QtGui.QMessageBox()
		msgBox.setText(message)
		msgBox.exec_()
        
        ## UTILITY METHODS ##

        
        def updateStats(self):
		self.emcStat.poll()
		for widget in self.widgetList:
			widget.updateData()
					
			
	def getEmcCom(self):
		return self.emcCom
	
	def getEmcStat(self):
		return self.emcStat	

        
        ## LINUXCNC INTERFACE ##
        
        def setestop(self):
        	if self.emcStat.estop == 0:
        		self.emcCom.state(linuxcnc.STATE_ESTOP)
        	elif self.emcStat.estop == 1:
        		self.emcCom.state(linuxcnc.STATE_ESTOP_RESET)
        		
	def setPower(self):
		if self.emcStat.enabled == False and self.emcStat.estop == 0:
        		self.emcCom.state(linuxcnc.STATE_ON)
        	elif self.emcStat.enabled == True and self.emcStat.estop == 0:
        		self.emcCom.state(linuxcnc.STATE_OFF)

        def homeAllAxis(self):
        	if self.emcStat.estop == 0 and self.emcStat.enabled == True and self.emcStat.interp_state == linuxcnc.INTERP_IDLE:
        		if self.emcStat.task_mode == linuxcnc.MODE_MANUAL:
        			self.emcCom.home(0)
        			self.emcCom.home(1)
        			self.emcCom.home(2)
        		else: 
				self.showMessage("Must be in manul mode to home axis")
				
	def homeAxis(self, axis):
        	if self.emcStat.estop == 0 and self.emcStat.enabled == True and self.emcStat.interp_state == linuxcnc.INTERP_IDLE:
        		if self.emcStat.task_mode == linuxcnc.MODE_MANUAL:
        			self.emcCom.home(axis)
        		else:
        			self.showMessage("Must be in manul mode to home axis")
        			
        def jogAxis(self, mode, axis, direction, velocity):
        	if mode == "continious":
        		self.emcCom.jog(linuxcnc.JOG_CONTINUOUS, axis, velocity*direction)
        	if mode == "1.00":
        		self.emcCom.jog(linuxcnc.JOG_INCREMENT, axis, velocity*direction, 1.00)
        	if mode == "0.10":
        		self.emcCom.jog(linuxcnc.JOG_INCREMENT, axis, velocity*direction, 0.10)
        	if mode == "0.01":
        		self.emcCom.jog(linuxcnc.JOG_INCREMENT, axis, velocity*direction, 0.01)
        
        def stopJog(self, axis):
        	self.emcCom.jog(linuxcnc.JOG_STOP, axis)

		      


if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
    	window = MainWindow()
    	window.show()
    	sys.exit(app.exec_())
